package data

import (
	_ "database/sql"
	"errors"
	"fmt"
	"gitchina/kly_service/util"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"strconv"
)

type LinebillDto struct {
	Id           int    `orm:"pk;auto;column(id)"`
	OwnerId      int    `orm:"column(owner_id)"`
	EmpId        int    `orm:"column(emp_id)"`
	LinePubId    int    `orm:"column(line_pub_id)"`
	ShipmentTime string `orm:"column(shipment_time)"`
	StartArea    int    `orm:"column(start_area)"`
	StartAddr    string `orm:"column(start_addr)"`
	EndArea      int    `orm:"column(end_area)"`
	EndAddr      string `orm:"column(end_addr)"`
	CargoName    string `orm:"column(cargo_name)"`
	CargoType    int    `orm:"column(cargo_type)"`
	CargoBulk    int    `orm:"column(cargo_bulk)"`
	CargoWeight  int    `orm:"column(cargo_weight)"`
	CargoCount   int    `orm:"column(cargo_count)"`
	Consignor    string `orm:"column(consignor)"`
	Consignee    string `orm:"column(consignee)"`
	DeliveryType int    `orm:"column(delivery_type)"`
	State        int    `orm:"column(state)"`
	Remark       string `orm:"column(remark)"`
}

func init() {
	orm.RegisterModel(new(LinebillDto))
}

func (dto *LinebillDto) TableName() string {
	return "o_linebill"
}

func (repo OrderRepo) MakeLinebill(dto *LinebillDto) (id int64, err error) {
	var num int64
	var list orm.ParamsList
	o := orm.NewOrm()
	num, err = o.Raw(SQL_Order_Ensure_Can_Make_Linebill, dto.LinePubId).ValuesFlat(&list)
	if num == 1 {
		id, err = o.Insert(dto)
	} else {
		if err == nil {
			err = fmt.Errorf("line id %d not found", dto.LinePubId)
		}
	}
	return
}

func (repo OrderRepo) GetLinebillDetail(id int) (dto LinebillDto, err error) {
	dto.Id = id
	o := orm.NewOrm()
	err = o.Read(&dto)
	return
}

func (repo OrderRepo) GetLinebillList(id, pageNo, pageSize int, queryState, mbrType string) ([]LinebillDto, int, error) {
	var sql string
	var rowCountSql string
	var num int64
	var rowCount int
	var err error
	var flat orm.ParamsList
	var linebillList []LinebillDto
	o := orm.NewOrm()

	var stateWhere string
	if queryState == "1" {
		stateWhere = " AND state = 0"
	} else if queryState == "2" {
		stateWhere = " AND state IN (1, 10)"
	}

	if mbrType == "1" {
		sql = fmt.Sprintf(SQL_Order_Get_Linebill_By_Owner, stateWhere, (pageNo-1)*pageSize, pageSize)
		rowCountSql = fmt.Sprintf(SQL_Order_Get_Linebill_By_Owner_Count, stateWhere)
	} else if mbrType == "3" {
		sql = fmt.Sprintf(SQL_Order_Get_Linebill_By_Emp, stateWhere, (pageNo-1)*pageSize, pageSize)
		rowCountSql = fmt.Sprintf(SQL_Order_Get_Linebill_By_Emp_Count, stateWhere)
	} else {
		return nil, 0, errors.New("invalid member type")
	}
	if num, err = o.Raw(sql, id).QueryRows(&linebillList); num >= 1 {
		if num, err = o.Raw(rowCountSql, id).ValuesFlat(&flat); num >= 1 {
			rowCount, err = strconv.Atoi(flat[0].(string))
			util.GetLogger().Debug("[data-GetLinebillList] - debug:rowcount::%s", flat[0])
		}
	}
	return linebillList, rowCount, err
}

func (repo OrderRepo) UpdateLinebillState(linebillId, memberId, state int) error {
	o := orm.NewOrm()
	_, err := o.Raw(SQL_Order_Upd_Linebill_State, state, linebillId, memberId).Exec()
	return err
}
